package com.goldze.mvvmhabit.ui.expandswipe;

import android.graphics.drawable.NinePatchDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.databinding.FragmentExpandSwipeRvBinding;
import com.goldze.mvvmhabit.widght.expandswipe.ExpandSwipeRvAdapter;
import com.h6ah4i.android.widget.advrecyclerview.animator.GeneralItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.animator.SwipeDismissItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager;
import com.h6ah4i.android.widget.advrecyclerview.expandable.RecyclerViewExpandableItemManager;
import com.h6ah4i.android.widget.advrecyclerview.swipeable.RecyclerViewSwipeManager;
import com.h6ah4i.android.widget.advrecyclerview.touchguard.RecyclerViewTouchActionGuardManager;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

public class ExpandSwipeFragment extends BaseFragment<FragmentExpandSwipeRvBinding, ExpandSwipeRvViewModel>
        implements RecyclerViewExpandableItemManager.OnGroupCollapseListener,RecyclerViewExpandableItemManager.OnGroupExpandListener {
    private static final String SAVED_STATE_EXPANDABLE_ITEM_MANAGER = "RecyclerViewExpandableItemManager";
    private LinearLayoutManager linearLayoutManager;
    private RecyclerViewExpandableItemManager mRecyclerViewExpandableItemManager;
    private RecyclerViewTouchActionGuardManager mRecyclerViewTouchActionGuardManager;
    private RecyclerViewDragDropManager mRecyclerViewDragDropManager;
    private RecyclerViewSwipeManager mRecyclerViewSwipeManager;
    private RecyclerView.Adapter mWrappedAdapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_expand_swipe_rv;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        KLog.e("sss 11");
        viewModel.initData();
        linearLayoutManager = new LinearLayoutManager(requireContext());
        mRecyclerViewExpandableItemManager = new RecyclerViewExpandableItemManager(null);
       mRecyclerViewExpandableItemManager.setOnGroupExpandListener(this);
        mRecyclerViewExpandableItemManager.setOnGroupCollapseListener(this);

        mRecyclerViewTouchActionGuardManager = new RecyclerViewTouchActionGuardManager();
        mRecyclerViewTouchActionGuardManager.setInterceptVerticalScrollingWhileAnimationRunning(true);
        mRecyclerViewTouchActionGuardManager.setEnabled(true);

        mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
        mRecyclerViewDragDropManager.setDraggingItemShadowDrawable(
                (NinePatchDrawable) ContextCompat.getDrawable(requireContext(), R.drawable.material_shadow_z3));

        // swipe manager
        mRecyclerViewSwipeManager = new RecyclerViewSwipeManager();

        ExpandSwipeRvAdapter swipeRvAdapter = new ExpandSwipeRvAdapter(mRecyclerViewExpandableItemManager);

        swipeRvAdapter.setItems(viewModel.list);
        swipeRvAdapter.setTtemBinding(viewModel.parentBinding,viewModel.childBinding);

        mWrappedAdapter = mRecyclerViewExpandableItemManager.createWrappedAdapter(swipeRvAdapter);       // wrap for expanding
        mWrappedAdapter = mRecyclerViewDragDropManager.createWrappedAdapter(mWrappedAdapter);           // wrap for dragging
        mWrappedAdapter = mRecyclerViewSwipeManager.createWrappedAdapter(mWrappedAdapter);      // wrap for swiping

        final GeneralItemAnimator animator = new SwipeDismissItemAnimator();

        // Change animations are enabled by default since support-v7-recyclerview v22.
        // Disable the change animation in order to make turning back animation of swiped item works properly.
        // Also need to disable them when using animation indicator.
        animator.setSupportsChangeAnimations(false);


        binding.rvExpandSwipe.setLayoutManager(linearLayoutManager);
        binding.rvExpandSwipe.setAdapter(mWrappedAdapter);  // requires *wrapped* adapter
        binding.rvExpandSwipe.setItemAnimator(animator);
        binding.rvExpandSwipe.setHasFixedSize(false);


        mRecyclerViewTouchActionGuardManager.attachRecyclerView( binding.rvExpandSwipe);
        mRecyclerViewSwipeManager.attachRecyclerView( binding.rvExpandSwipe);
        mRecyclerViewDragDropManager.attachRecyclerView( binding.rvExpandSwipe);
        mRecyclerViewExpandableItemManager.attachRecyclerView( binding.rvExpandSwipe);




    }

    @Override
    public void onGroupCollapse(int groupPosition, boolean fromUser, Object payload) {
        KLog.e("sss  "+groupPosition+"  "+fromUser+"  ");
    }

    @Override
    public void onGroupExpand(int groupPosition, boolean fromUser, Object payload) {
        KLog.e("sss  "+groupPosition+"  "+fromUser+"  ");
        if (fromUser) {
            adjustScrollPositionOnGroupExpanded(groupPosition);
        }
    }
    private void adjustScrollPositionOnGroupExpanded(int groupPosition) {
        int childItemHeight = getActivity().getResources().getDimensionPixelSize(R.dimen.list_item_height);
        int topMargin = (int) (getActivity().getResources().getDisplayMetrics().density * 16); // top-spacing: 16dp
        int bottomMargin = topMargin; // bottom-spacing: 16dp

        mRecyclerViewExpandableItemManager.scrollToGroup(groupPosition, childItemHeight, topMargin, bottomMargin);
    }


}
